package com.ilvyou.api.service;

import com.ilvyou.api.bean.result.MainInfo;
import com.ilvyou.api.util.LhfUtil;
import com.ilvyou.api.util.RemoteUtil;
import com.ilvyou.core.base.BaseService;
import com.ilvyou.core.dao.Criteria;
import com.ilvyou.core.dao.Dao;
import com.ilvyou.core.dao.Query;
import com.ilvyou.core.util.StringUtil;
import com.ilvyou.core.util.Util;
import com.ilvyou.data.lhf.*;
import com.ilvyou.data.lzclub.MonRewardEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * Created by GuanYuCai on 2016/9/8 0008.
 */
@Service
public class FinanceService extends BaseService {

    @Resource(name = "lhfDao")
    private Dao lhfDao;

    @Resource(name = "zanClubDao")
    private Dao zanClubDao;

    @Autowired
    private ZanService systemService;

    @Override
    protected Dao getDao() {
        return lhfDao;
    }

    public MainInfo getMainInfo(long actId) throws Exception {
        MainInfo rs = new MainInfo();
        AlipayAccountEntity account = getByPK(AlipayAccountEntity.class, actId);
        ScrAlipayEntity alipay = lhfDao.getByPK(ScrAlipayEntity.class, actId);

        MonRewardEntity monReward = null;
        if(!StringUtil.isEmpty(alipay.getBpploginname())){
            monReward = zanClubDao.get(MonRewardEntity.class,
                    new Criteria().add("userid=?", alipay.getBppuserid()));
        }

        rs.setIdentity(alipay.getBppagentcategory());
        rs.setMobile(alipay.getMobile());
        rs.setCardqty(account.getCardqty());
        rs.setBpploginname(alipay.getBpploginname() != null ? alipay.getBpploginname() : "");
        rs.setPlatactname(StringUtil.isEmpty(alipay.getPlatactname()) ? "" : alipay.getPlatactname());
        rs.setFreezeamount(account.getFreezeamount());
        rs.setLeftamount(account.getRightamount());//余额
        rs.setTotalassets(account.getRightamount());//总资产
        rs.setCouponamount(monReward == null ? 0.0 : monReward.getUsableamount());

        rs.setTransferout(Double.parseDouble(StringUtil.toCurrency(getTransfer(actId, false))));
        rs.setTransferin(Double.parseDouble(StringUtil.toCurrency(getTransfer(actId, true))));

        return rs;
    }

    public AlipayPeriodEntity getAlipayPeriod(Long actId) throws Exception{
        return lhfDao.get(AlipayPeriodEntity.class,
                new Criteria("actid=? and periodmonth=?", actId, LhfUtil.getPeriodMonth()));
    }

    public void addWithdraw(ScrAlipayEntity alipay, AlipayAccountEntity account, AlipayPeriodEntity period,
                            AlipayPbankEntity pbank, double amount, String sourceType) throws Exception{
        BigDecimal leftAmount = new BigDecimal(account.getLeftamount()); //账户总资产
        BigDecimal amountPrice = new BigDecimal(amount);
        BigDecimal rightAmount = new BigDecimal(account.getRightamount());
        //提现后的账户余额
        BigDecimal rightAmountNew = rightAmount.subtract(amountPrice);
        //生成流水表中的logid
        long reflogid = Util.createId();
        //向流水表alipay_log中添加一条记录
        lhfDao.insert(createAlipayLog(period, alipay, leftAmount, rightAmount, amountPrice, reflogid, "", "Drawal", "Out" , sourceType));

        //生成提现表中的logid
        long logid = Util.createId();
        //向提现表alipay_withdraw中添加一条记录
        insertAlipayWithdraw(period, leftAmount, rightAmount, amountPrice, alipay, pbank, logid, reflogid, sourceType);

        //生成资金减项表中的logid
        long delogid = Util.createId();
        //向alipay_decrease 资金减项表增加一条数据
        AlipayDecreaseEntity decrease = createDecrease(account, account, period, amountPrice, delogid, reflogid, 0, "", "Drawal", "");
        lhfDao.insert(decrease);

        // 封装更新增项表和对增减关系表进行插入操作
        // 不执行该项操作 guanyucai 2016.10.13
//        setIncrease(alipay.getActid(), decrease, amount);

        //alipay_account 更新账户余额
        account.setLeftamount(new BigDecimal(account.getLeftamount()).subtract(amountPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        account.setRightamount(rightAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        account.setLastedon(new Timestamp(System.currentTimeMillis()));
        lhfDao.update(account);
    }

    public long addTransfer(ScrAlipayEntity alipayOut, ScrAlipayEntity alipayIn, AlipayAccountEntity accountOut,
                            AlipayAccountEntity accountIn, AlipayPeriodEntity periodOut, AlipayPeriodEntity periodIn,
                            double money, String note, String sourceType) throws Exception{
        //转出账户的总资产
        BigDecimal leftAmount = new BigDecimal(accountOut.getLeftamount());
        //转入账户的总资产
        BigDecimal tLeftAmount = new BigDecimal(accountIn.getLeftamount());
        BigDecimal rightAmount = new BigDecimal(accountOut.getRightamount());
        //转入账户的账户余额
        BigDecimal tRightAmount = new BigDecimal(accountIn.getRightamount());
        BigDecimal amountPrice = new BigDecimal(money);

        long inreflogid = Util.createId();       //生成转入流水logid
        long outreflogid = Util.createId();   //生成转出流水logid
        long logid = Util.createId();       //转账流水logid
        long orderid = Util.createId();     //获取订单id
        String lorderNum = systemService.getSequenceName("ALIPAY_TRANSFER", "LORDERNUM");     //获取转账的订单编号

        //向转账表格alipay_transfer中添加一条记录
        lhfDao.insert(createTransfer(periodOut, alipayOut, periodIn, alipayIn, leftAmount, tLeftAmount, rightAmount, tRightAmount, amountPrice, logid, lorderNum,
                inreflogid, outreflogid, note, sourceType));

        //添加转出的流水alipay_log
        lhfDao.insert(createAlipayLog(periodOut, alipayOut, leftAmount, rightAmount,
                amountPrice, outreflogid, lorderNum, "Transfer", "Out", sourceType));

        //添加转入的流水alipay_log
        lhfDao.insert(createAlipayLog(periodIn, alipayIn, tLeftAmount, tRightAmount,
                amountPrice, inreflogid, lorderNum, "Transfer", "In", sourceType));

        long logidin = Util.createId();      //生成资金增向表logid
        long logidout = Util.createId();  //生成资金减项表logid
        //向资金增项表格alipay_increase中添加一条记录
        lhfDao.insert(createIncrease(accountOut, accountIn, periodIn, logidin, amountPrice, inreflogid, "Transfer", note));

        //向资金减项表格alipay_decrease中添加一条记录
        AlipayDecreaseEntity decrease = createDecrease(accountIn, accountOut, periodOut,
                amountPrice, logidout, outreflogid, orderid, lorderNum,  "Transfer", note);
        lhfDao.insert(decrease);

        // 封装更新增项表和对增减关系表进行插入操作
        // 不执行该项操作 guanyucai 2016.10.13
//        setIncrease(accountOut.getActid() , decrease, money);

        //更新转出账户的alipay_account余额
        accountOut.setRightamount(rightAmount.subtract(amountPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        accountOut.setLeftamount(leftAmount.subtract(amountPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        accountOut.setLastedon(new Timestamp(System.currentTimeMillis()));
        lhfDao.update(accountOut);

        //更新转入账户的alipay_account余额
        accountIn.setLeftamount(tLeftAmount.add(amountPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        accountIn.setRightamount(tRightAmount.add(amountPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        accountIn.setLastedby(accountOut.getActid());
        accountIn.setLastedname(accountOut.getActname());
        accountIn.setLastedon(new Timestamp(System.currentTimeMillis()));
        lhfDao.update(accountIn);

        return logid;
    }

    public void sendTransferMessage(AlipayTransferEntity transfer) throws Exception{
        ScrAlipayEntity from = getByPK(ScrAlipayEntity.class, transfer.getFactid());
        ScrAlipayEntity to = getByPK(ScrAlipayEntity.class, transfer.getTactid());
        String date = LhfUtil.toStr(transfer.getTransferdate(), "M月d日 HH:mm");
        String amount = StringUtil.toCurrency(transfer.getPaidamount());
        String message = "尊敬的用户：您好，您于"+ date +"完成一笔向"+ transfer.getTactname() + "的资金转账，转账金额" + amount + "元，可用余额" +
                StringUtil.toCurrency(transfer.getFnrightamount()) + "元。如有疑问，欢迎致电400-114-6969。【乐汇付】—";
        RemoteUtil.sendSMS(from.getMobile(), message);

        message = "尊敬的用户：您好，您于" + date + "收到来自" + transfer.getFactname() + "一笔资金转账，收款金额 " + amount + "元，可用余额" +
                StringUtil.toCurrency(transfer.getTnrightamount()) + "元。如有疑问，欢迎致电400-114-6969。【乐汇付】—";
        RemoteUtil.sendSMS(to.getMobile(), message);
    }

    private double getTransfer(long actId, boolean in) throws Exception{
        Criteria criteria = new Criteria().add(in ? "tactid=?" : "factid=?", actId);
        Map<String, Object> map = lhfDao.getMap(new Query(AlipayTransferEntity.class, "count(*) as c")
                .where(criteria));
        long count = (Long) map.get("c");

        if (count > 0){
            map = lhfDao.getMap(AlipayTransferEntity.class, "sum(payamount) as s", criteria);
            return (Double) map.get("s");
        }
        return 0;
    }

    private AlipayLogEntity createAlipayLog(AlipayPeriodEntity period, ScrAlipayEntity alipay, BigDecimal leftamount, BigDecimal rightamount, BigDecimal amountPrice,
                           long logid, String lorderNum, String tradeType, String tradeFlow, String sourceType) throws Exception {
        BigDecimal leftAmountNew = new BigDecimal(0.00);
        BigDecimal rightAmountNew = new BigDecimal(0.00);
        BigDecimal inAmount = new BigDecimal(0.00);
        String tradedigest = "";
        if("Out".equals(tradeFlow)){
            leftAmountNew = leftamount.subtract(amountPrice);
            rightAmountNew = rightamount.subtract(amountPrice);
        }else if("In".equals(tradeFlow)){
            leftAmountNew = leftamount.add(amountPrice);
            rightAmountNew = rightamount.add(amountPrice);
            inAmount = amountPrice;
        }
        if("Transfer".equals(tradeType)){
            tradedigest = "转账";
        }else if("Drawal".equals(tradeType)){
            tradedigest = "提现";
        }

        AlipayLogEntity log = new AlipayLogEntity();
        Util.transfer(alipay, log, "actid,actname,realname,bppusercode,bpploginname,bppcompanyname,bppcompanyfname," +
                "bppuserid,bppcompanyid,bppcompanycode,bppaccounttype,platactname,platactindex,storesname,storeid,storescode");
        log.setCreatedname(alipay.getActname());
        log.setLastedname(alipay.getActname());
        log.setCreatedby(alipay.getActid());
        log.setLastedby(alipay.getActid());
        Util.transfer(period, log, "periodid,periodmonth,periodbegindate,periodenddate");
        log.setOleftamount(leftamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setNleftamount(leftAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setOrightamount(rightamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setNrightamount(rightAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setLogid(logid);
        log.setTradeflow(tradeFlow);
        log.setTradetype(tradeType);
        log.setOrdertype((short) ("Transfer".equals(tradeType) ? 2 : 1));
        log.setOrderid(0l);
        log.setOrdernum(lorderNum);
        log.setTradedigest(tradedigest);
        log.setTradestatus("Transfer".equals(tradeType) ? "2" : "1");
        log.setDeleteflag((short) 0);
        log.setTradeamount(amountPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setInamount(inAmount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setSourcetype(sourceType);
        return log;
    }

    private void insertAlipayWithdraw(AlipayPeriodEntity period, BigDecimal leftamount, BigDecimal rightamount,
                                      BigDecimal amountPrice, ScrAlipayEntity alipay, AlipayPbankEntity pbank, long logid, long reflogid,
                           String sourceType) throws Exception {
        BigDecimal leftAmountNew, rightAmountNew;
        leftAmountNew = leftamount.subtract(amountPrice);
        rightAmountNew = rightamount.subtract(amountPrice);

        AlipayWithdrawEntity log = new AlipayWithdrawEntity();
        Util.transfer(period, log, "periodid,periodmonth,periodbegindate,periodenddate");
        Util.transfer(alipay, log, "actid,actname,realname,bppusercode,bpploginname,bppcompanyname,bppcompanyfname," +
                "bppuserid,bppcompanyid,bppcompanycode,bppaccounttype,platactname,platactindex,storeid,storescode,storesname");
        log.setCreatedname(alipay.getActname());
        log.setLastedname(alipay.getActname());
        log.setCreatedby(alipay.getActid());
        log.setLastedby(alipay.getActid());
        log.setOleftamount(leftamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setNleftamount(leftAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setOrightamount(rightamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setNrightamount(rightAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setAmount(amountPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        log.setApplicantname(("1").equals(pbank.getAccounttype()) ? pbank.getPname() : pbank.getCompanyfname() != null ? pbank.getCompanyfname() : pbank.getCompanyaccount());
        log.setApplicantmobile("1".equals(pbank.getAccounttype()) ? pbank.getPmobile() : pbank.getCompanymobile());
        log.setLogid(logid);
        log.setAccreditnum("1".equals(pbank.getAccounttype()) ? "个人账户" : "对公账户");
        log.setReflogid(reflogid);
        log.setTicketdays(45);
        log.setNotifyflag((short) 1);
        log.setIntamount(0.0);
        log.setTradestatus((short) 1);
        log.setTicketoutcost(0.0);
        log.setDeleteflag((short) 0);
        log.setSubmitflag((short) 0);
        Util.transfer(pbank, log, "bankid,bankname,bankcode");
        log.setBanknum(pbank.getBanknumber());
        log.setSourcetype(sourceType);
        lhfDao.insert(log);
    }

    private AlipayDecreaseEntity createDecrease(AlipayAccountEntity from, AlipayAccountEntity to, AlipayPeriodEntity period, BigDecimal amountPrice, long logid,
                           long reflogid, long orderid, String orderNum, String tradeType, String remark) throws Exception {
        long orderId = 0;
        String sourceType, note, lorderNum = "";
        if("Transfer".equals(tradeType)){
            orderId = orderid;
            lorderNum = orderNum;
            sourceType = "2";
            note = remark != null ? remark : "转账";
        }else if("Drawal".equals(tradeType)){
            sourceType = "4";
            note = remark != null ? remark : "账户提现";
        }else if("Coupon".equals(tradeType)){
            sourceType = "5";
            note = remark != null ? remark : "购买点券";
        }else{
            sourceType = "4";
            note = remark != null ? remark : "";
        }

        AlipayDecreaseEntity log = new AlipayDecreaseEntity();
        log.setFactid(from.getActid());
        log.setFactname(from.getActname());
        log.setFrealname(from.getRealname());
        log.setTactid(to.getActid());
        log.setTactname(to.getActname());
        log.setTrealname(to.getRealname());
        log.setCreatedname(to.getActname());
        log.setCreatedby(to.getActid());
        log.setLastedname(to.getActname());
        log.setLastedby(to.getActid());
        Util.transfer(period, log, "periodmonth,periodbegindate,periodenddate");
        log.setLogid(logid);
        log.setReflogid(reflogid);
        log.setOrderid(orderId);
        log.setOrdernum(lorderNum);
        log.setSourcetype(sourceType);
        log.setRemark(note);
        log.setAmount(amountPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        return log;
    }

    private AlipayIncreaseEntity createIncrease(AlipayAccountEntity from, AlipayAccountEntity to,
                           AlipayPeriodEntity period, long logid,
                           BigDecimal amountPrice, long reflogid,
                           String tradeType, String remark) throws Exception {
        String sourceType, note;
        if("Transfer".equals(tradeType)){
            sourceType = "2";
            note = "转账";
        }else if("Drawal".equals(tradeType)){
            sourceType = "4";
            note = "提现";
        }else{
            sourceType = "4";
            note = remark;
        }

        AlipayIncreaseEntity i = new AlipayIncreaseEntity();
        i.setFactid(from.getActid());
        i.setFactname(from.getActname());
        i.setFrealname(from.getRealname());
        i.setCreatedname(from.getActname());
        i.setCreatedby(from.getActid());
        i.setLastedby(from.getActid());
        i.setLastedname(from.getActname());

        i.setTactid(to.getActid());
        i.setTactname(to.getActname());
        i.setTrealname(to.getRealname());

        Util.transfer(period, i, "periodmonth,periodbegindate,periodenddate");

        i.setLogid(logid);
        i.setAmount(amountPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        i.setSurplusamount(amountPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        i.setReflogid(reflogid);
        i.setSourcetype(sourceType);
        i.setRemark(note);
        return i;
    }

    private void setIncrease(long actId, AlipayDecreaseEntity decrease, double amount) throws Exception{
        double surplurAmount;
        BigDecimal leftAmount;
        //根据用户Id(actId)查询用户的增项表记录
        Query query = new Query(AlipayIncreaseEntity.class, "*")
                .where(new Criteria("tactid=? and surplusamount > 0.0", actId))
                .order("createdon");
        List<Map<String, Object>> increaseList = lhfDao.getMapList(query);
        for(Map<String, Object> map : increaseList){
            AlipayIncreaseEntity increase = Util.fromMap(map, AlipayIncreaseEntity.class);
            amount -= increase.getSurplusamount();
            surplurAmount = amount >= 0.00 ? 0.00 : (-amount);
            leftAmount = new BigDecimal(surplurAmount);
            increase.setSurplusamount(leftAmount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            increase.setLastedby(actId);
            increase.setLastedon(new Timestamp(System.currentTimeMillis()));
            increase.setLastedname(decrease.getLastedname());
            //更新增项表中的剩余金额记录
            lhfDao.update(increase);
            //向增减关系表中插入一条记录
            insertInandde(Util.createId(), increase, decrease);

            if (amount <= 0){
                return;
            }
        }
    }

    private void insertInandde(long pkId, AlipayIncreaseEntity increase, AlipayDecreaseEntity decrease) throws Exception {
        AlipayInanddeEntity log = new AlipayInanddeEntity();
        log.setPkid(pkId);
        log.setInlogid(increase.getLogid());
        log.setDelogid(decrease.getLogid() + "");
        log.setInfactid(increase.getFactid());
        log.setInfactname(increase.getFactname());
        log.setInfrealname(increase.getFrealname());
        log.setIntactid(increase.getTactid());
        log.setIntactname(increase.getTactname());
        log.setIntrealname(increase.getTrealname());
        log.setDefactid(decrease.getFactid());
        log.setDefactname(decrease.getFactname());
        log.setDefrealname(decrease.getFrealname());
        log.setDetactid(decrease.getTactid());
        log.setDetactname(decrease.getTactname());
        log.setDetrealname(decrease.getTrealname());
        log.setPeriodmonth(increase.getPeriodmonth());
        log.setPeriodbegindate(increase.getPeriodbegindate());
        log.setPeriodenddate(increase.getPeriodenddate());
        log.setInamount(increase.getAmount());
        log.setIncreasedate(increase.getIncreasedate());
        log.setSurplusamount(increase.getSurplusamount());
        log.setInreflogid(increase.getReflogid());
        log.setInsourcetype(increase.getSourcetype());
        log.setDeamount(decrease.getAmount());
        log.setDecreasedate(decrease.getDecreasedate());
        log.setDereflogid(decrease.getReflogid());
        log.setDesourcetype(decrease.getSourcetype());
        log.setCreatedname(decrease.getCreatedname());
        log.setCreatedby(decrease.getCreatedby());
        lhfDao.insert(log);
    }

    private AlipayTransferEntity createTransfer(AlipayPeriodEntity periodFrom, ScrAlipayEntity alipayFrom,
                           AlipayPeriodEntity periodTo, ScrAlipayEntity alipayTo, BigDecimal leftamount,
                           BigDecimal tleftamount, BigDecimal rightamount, BigDecimal trightamount,
                           BigDecimal amountPrice, long logid, String lorderNum,
                           long inreflogid, long outreflogid, String remark, String sourceType) throws Exception {
        BigDecimal leftAmountNew, tLeftAmountNew, rightAmountNew, tRightAmountNew;
        leftAmountNew = leftamount.subtract(amountPrice);
        rightAmountNew = rightamount.subtract(amountPrice);
        tLeftAmountNew = tleftamount.add(amountPrice);
        tRightAmountNew = trightamount.add(amountPrice);

        AlipayTransferEntity t = new AlipayTransferEntity();
        t.setFactid(alipayFrom.getActid());
        t.setFactname(alipayFrom.getActname());
        t.setFrealname(alipayFrom.getRealname());
        t.setFromcompanyid(alipayFrom.getBppcompanyid());
        t.setFromcompanycode(alipayFrom.getBppcompanycode());
        t.setFromcompanyshortname(alipayFrom.getBppcompanyname());
        t.setFromcompanyfullname(alipayFrom.getBppcompanyfname());
        t.setPayname(alipayFrom.getActname());
        t.setCreatedname(alipayFrom.getActname());
        t.setLastedname(alipayFrom.getActname());
        t.setCreatedby(alipayFrom.getActid());
        t.setLastedby(alipayFrom.getActid());
        t.setFrombppuserid(alipayFrom.getBppuserid());
        t.setFrombppusercode(alipayFrom.getBppusercode());
        t.setFrombpploginname(alipayFrom.getBpploginname());
        t.setFromplatactindex(alipayFrom.getPlatactindex());
        int i = alipayFrom.getBppaccounttype() == null ? 0 : alipayFrom.getBppaccounttype();
        t.setFrombppaccounttype(i);
        t.setFromplatactname(alipayFrom.getPlatactname());
        t.setFromstoreid(alipayFrom.getStoreid());
        t.setFromstorescode(alipayFrom.getStorescode());
        t.setFromstoresname(alipayFrom.getStoresname());
        t.setFrombppagentcategory(alipayFrom.getBppagentcategory());

        t.setTocompanyid(alipayTo.getBppcompanyid());
        t.setTocompanycode(alipayTo.getBppcompanycode());
        t.setTocompanyshortname(alipayTo.getBppcompanyname());
        t.setTocompanyfullname(alipayTo.getBppcompanyfname());
        t.setTactid(alipayTo.getActid());
        t.setTactname(alipayTo.getActname());
        t.setTrealname(alipayTo.getRealname());
        t.setTobppuserid(alipayTo.getBppuserid());
        t.setTobppusercode(alipayTo.getBppusercode());
        t.setTobpploginname(alipayTo.getBpploginname());
        int j = alipayTo.getBppaccounttype() == null ? 0 : alipayTo.getBppaccounttype();
        t.setTobppaccounttype(j);
        t.setToplatactindex(alipayTo.getPlatactindex());
        t.setToplatactname(alipayTo.getPlatactname());
        t.setTostoreid(alipayTo.getStoreid());
        t.setTostorescode(alipayTo.getStorescode());
        t.setTostoresname(alipayTo.getStoresname());
        t.setTobppagentcategory(alipayTo.getBppagentcategory());

        t.setFperiodid(periodFrom.getPeriodid());
        t.setFperiodmonth(periodFrom.getPeriodmonth());
        t.setFperiodbegindate(periodFrom.getPeriodbegindate());
        t.setFperiodenddate(periodFrom.getPeriodenddate());

        t.setTperiodid(periodTo.getPeriodid());
        t.setTperiodmonth(periodTo.getPeriodmonth());
        t.setTperiodbegindate(periodTo.getPeriodbegindate());
        t.setTperiodenddate(periodTo.getPeriodenddate());

        t.setFoleftamount(leftamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setFnleftamount(leftAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setForightamount(rightamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setFnrightamount(rightAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setToleftamount(tleftamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setTnleftamount(tLeftAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setTorightamount(trightamount.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setTnrightamount(tRightAmountNew.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setOutreflogid(outreflogid);
        t.setInreflogid(inreflogid);
        t.setLogid(logid);
        t.setLordernum(lorderNum);
        t.setTradestatus((short) 2);
        t.setPayamount(amountPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setPaidamount(amountPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        t.setDistamount(0.0);
        t.setExpenseamount(0.0);
        t.setPaytype("1");
        t.setRemark(remark);
        t.setDeleteflag((short) 0);
        t.setSourcetype(sourceType);
        return t;
    }
}
